define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index' + location.search,
                    add_url: 'coupon/add',
                    // edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    import_url: 'coupon/import',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'discount_type', title: __('Discount_type'), searchList: {"1":__('Discount_type 1'),"2":__('Discount_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'goods_type', title: __('Goods_type'), searchList: {"all":__('Goods_type all'),"part":__('Goods_type part')}, formatter: Table.api.formatter.normal, operate: false},
                        // {field: 'goods_ids', title: __('Goods_ids'), operate: false},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'target', title: __('Target'), operate: false},
                        // {field: 'price', title: __('Price'), operate: false},
                        // {field: 'discount', title: __('Discount'), operate: false},
                        {field: 'sent_type', title: __('Sent_type'), searchList: {"send":__('Sent_type send'),"draw":__('Sent_type draw')}, formatter: Table.api.formatter.normal},
                        {field: 'use_type', title: __('Use_type'), searchList: {"mini":__('Use_type mini'),"pc":__('Use_type pc')}, operate: false, formatter: Table.api.formatter.label},
                        // {field: 'sent_user_ids', title: __('Sent_user_ids'), operate: false},
                        {field: 'sent_num', title: __('Sent_num'), operate: false},
                        {field: 'draw_num', title: __('Draw_num'), operate: false},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'time',
                            title: __('使用时间'),
                            operate: false,
                            formatter: function (value, row) {
                                return row.starttime_text +' - '+ row.endtime_text;
                            }
                        },
                        // {field: 'starttime', title: __('Starttime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'endtime', title: __('Endtime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    $(".discount_type").change(function () {
        let discount_type = $(this).val();
        if(discount_type == 1){
            $(".price").removeClass('hide');
            $(".discount").addClass('hide');
        }else{
            $(".price").addClass('hide');
            $(".discount").removeClass('hide');
        }
    })

    $(".goods_type").change(function () {
        let goods_type = $(this).val();
        if(goods_type === 'part'){
            $(".goods_ids").removeClass('hide');
        }else{
            $(".goods_ids").addClass('hide');
        }
    })

    $(".sent_type").change(function () {
        let sent_type = $(this).val();
        if(sent_type === 'send'){
            $(".sent_user_ids").removeClass('hide');
        }else{
            $(".sent_user_ids").addClass('hide');
        }
    })

    return Controller;
});
